package zhuhai.tedu.parttimess.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import zhuhai.tedu.parttimess.domain.Result;
import zhuhai.tedu.parttimess.domain.Student;
import zhuhai.tedu.parttimess.domain.vo.StudentVo;
import zhuhai.tedu.parttimess.service.StudentService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/student/")
@Api(value = "学生")
public class StudentController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger("StudentController");

    @Autowired
    private StudentService studentService;

    @Autowired
    private HttpSession session;

    @ApiOperation(value = "分页查询")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页面大小", name = "size", paramType = "query"),
            @ApiImplicitParam(value = "当前页面", name = "page", paramType = "query"),
            @ApiImplicitParam(value = "用户id", name = "userId", paramType = "query"),
            @ApiImplicitParam(value = "用户名", name = "userName", paramType = "query")
    })
    private Page<StudentVo> findByPage(final StudentVo studentVo, Pageable pageable) {
        Page<Student> page = studentService.findByPage(studentVo.toStudent(), pageable);
        return page.map(m -> new StudentVo(m));
    }

    @ApiOperation(value = "添加学生")
    @RequestMapping(method = RequestMethod.POST)
    private Result add(@Valid @RequestBody StudentVo studentVo, final BindingResult result) {
        try {
            if (!result.hasErrors()) {
                studentService.save(studentVo.toStudent());
                return new Result("成功", 1);
            } else {
                return new Result("添加用户失败", 0);
            }
        } catch (Exception e) {
            LOG.error("添加用户失败", e);
            return new Result("添加用户失败", 0);
        }

    }

    @ApiOperation(value = "删除学生")
    @RequestMapping(value = "delete/{ids}", method = RequestMethod.DELETE)
    private Result delete(@PathVariable String ids) {
        String[] arr = ids.split(",");
        if (null != arr) {
            for (int i = 0; i < arr.length; i++) {
                studentService.deleteById(Integer.parseInt(arr[i]));
            }
            return new Result("success", 1);
        } else {
            return new Result("fail", 0);
        }
    }

    @ApiOperation(value = "修改学生")
    @RequestMapping(method = RequestMethod.PUT)
    private Result update(@Valid @RequestBody StudentVo studentVo, final BindingResult result) {
        try {
            if (!result.hasErrors()) {
                studentService.save(studentVo.toStudent());
                return new Result("修改学生成功", 1);
            } else {
                return new Result("修改学生失败", 0);
            }
        } catch (Exception e) {
            LOG.error("修改用户失败", e);
            return new Result("修改用户失败", 0);
        }

    }

    @ApiOperation(value = "导入学员数据", notes = "导入学员数据", consumes = "application/json")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public Result importProjectPatentExcel( @RequestParam(value = "file") MultipartFile file) {
        try {
            studentService.importStudentData(file);
        } catch (Exception e) {
            LOG.error("导入失败", e.getMessage());
            return new Result("导入失败", 0);
        }
        return new Result("导入成功", 1);
    }

    @ApiOperation(value = "导出学员数据", notes = "导出学员数据")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public Result exportStudentData(HttpServletResponse response){
        try {
            Integer userId=getUidFromSession(session);
            studentService.exportSutdentData(Integer.valueOf(userId),response);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result("导出成功",1);
    }
}



